//
//  YYT_SoundYueShowViewController.m
//  AudioVisualFeast
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YYT_SoundYueShowViewController.h"
#import "YYT_SoundYueShowNavigationView.h"
#import <MJRefresh.h>
#import "YYT_AFNTools.h"
#import "YYT_ModleShared.h"
#import "YYT_SoundYueShowView.h"
#import "YYT_MVPlayerViewController.h"

@interface YYT_SoundYueShowViewController ()<cellClick,SoundYueShowViewDelegate>
@property (nonatomic, retain) YYT_SoundYueShowNavigationView *daohang;
// 音乐节目的数组
@property (nonatomic, retain) NSMutableArray *SoundyuePremieredataSource;
// 网络请求拼得参数
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, retain) NSString *area;

@property (nonatomic, retain) YYT_SoundYueShowView *showView;
@end

@implementation YYT_SoundYueShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.area = @"%5B30971%2C36771%2C32393%2C21713%5D";
    // Do any additional setup after loading the view.
    self.showView = [[YYT_SoundYueShowView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49)];
    self.showView.SoundDelegate = self;
    [self.view addSubview:self.showView];
    
    self.daohang = [[YYT_SoundYueShowNavigationView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 44)];
    _daohang.delegate = self;
    self.daohang.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.daohang];
    
    // 初始化 mv页数组
    self.SoundyuePremieredataSource = [NSMutableArray arrayWithCapacity:0];
    // 网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // ALL 全部页面
        
        [self getData:self.num withArea:self.area];
    });
    
    
    [self addfooter];
    [self addheader];
    
}

#pragma mark -- 网络请求
-(void)getData1:(NSInteger)dataNum withArea:(NSString *)area{
    [YYT_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/video/list.json?D-A=0" parameter:@{@"promoTitle":@"true", @"area":area, @"offset":[NSString stringWithFormat:@"%ld", dataNum], @"size":@"20", @"supportBanner":@"true"} httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        NSArray *array = dictionary[@"videos"];
        for (NSDictionary *dic in array) {
            YYT_ModleShared *model = [[YYT_ModleShared alloc] initWithDictionary:dic];
            [self.SoundyuePremieredataSource addObject:model];
        }
        //  刷新tableView
        [_showView.tableview reloadData];
        
        self.showView.aaaaarray = self.SoundyuePremieredataSource;
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


- (void)getData:(NSInteger)dataNum withArea:(NSString *)area{
    [YYT_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/artist/much_videos.json?D-A=0" parameter:@{@"artistIds":area, @"offset":[NSString stringWithFormat:@"%ld", dataNum], @"size":@"20"} httpHeader:@{@"App-Id":@"10201031",@"Device-Id":@"1e71146b2b6211db3477eeea7538b2f9",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDE5MDAw"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        NSArray *array = dictionary[@"videos"];
        for (NSDictionary *dic in array) {
            YYT_ModleShared *model = [[YYT_ModleShared alloc] initWithDictionary:dic];
            [self.SoundyuePremieredataSource addObject:model];
        }
        // 刷新TableView
        [self.showView.tableview reloadData];
        
        self.showView.aaaaarray = self.SoundyuePremieredataSource;
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

#pragma mark -- 上拉刷新
-(void)addfooter {
    self.showView.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num += 20;
            [self getData:self.num withArea:self.area];
            [self.showView.tableview.mj_footer endRefreshing];
        });
    }];
    
}
#pragma mark -- 下拉刷新
-(void)addheader {
    self.showView.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num = 0;
            [self.SoundyuePremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
            [self.showView.tableview.mj_header endRefreshing];
        });
    }];
    [self.showView.tableview.mj_header beginRefreshing];
}

#pragma mark -- cellClick协议
-(void)cellClick:(NSInteger)item {
    
    //   音乐大来宾 %5B36771%5D
    //   star!调查团 %5B30971%5D
    //   周榜播报 %5B21713%5D
    //   V榜特别企划 %5B32393%5D
    //   全部 %5B30971%2C36771%2C32393%2C21713%5D
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (item == 5 || item == 11 || item == 17) {
            self.area = @"ALL";
            [self.SoundyuePremieredataSource removeAllObjects];
            [self getData1:self.num withArea:self.area];
        } else if (item == 0 || item == 6 || item == 12) {
            self.area = @"%5B30971%2C36771%2C32393%2C21713%5D";
            [self.SoundyuePremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 1 || item == 7 || item == 13) {
            self.area = @"%5B21713%5D";
            [self.SoundyuePremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 2 || item == 8 || item == 14) {
            self.area = @"%5B32393%5D";
            [self.SoundyuePremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 3 || item == 9 || item == 15) {
            self.area = @"%5B36771%5D";
            [self.SoundyuePremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        } else if (item == 4 || item == 10 || item == 16) {
            self.area = @"%5B30971%5D";
            [self.SoundyuePremieredataSource removeAllObjects];
            [self getData:self.num withArea:self.area];
        }
        [_showView.tableview setContentOffset:CGPointMake(0, 0)];
    });
}

- (void)pushToSoundDetailVCtableview:(UITableView *)tableview didSelectIndexPath:(NSIndexPath *)indexPath{
    YYT_MVPlayerViewController *mvPlayerVC = [[YYT_MVPlayerViewController alloc]init];
    mvPlayerVC.playurl = [self.SoundyuePremieredataSource[indexPath.row]urls]; // 传一个每个cell 相对应的网址、
    mvPlayerVC.headtitle = [self.SoundyuePremieredataSource[indexPath.row] title];// 传一个标题
    mvPlayerVC.ids = [NSString stringWithFormat:@"%@", [self.SoundyuePremieredataSource[indexPath.row] ids]]; //id
    mvPlayerVC.pic = [NSString stringWithFormat:@"%@",[self.SoundyuePremieredataSource[indexPath.row] playListPic]]; // 图片
    [self.navigationController pushViewController:mvPlayerVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
